package cn.waka.sys.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import cn.waka.sys.admin.object.Page;
import cn.waka.sys.admin.object.ResourceData;
import cn.waka.sys.admin.object.Resources;
import cn.waka.sys.admin.object.Result;
import cn.waka.sys.admin.object.Scene;
import cn.waka.sys.admin.service.BoxService;
import cn.waka.sys.admin.service.DicService;
import cn.waka.sys.admin.service.ResourceService;
import cn.waka.sys.admin.utils.Validator;

@RestController
@RequestMapping("/resource")
public class ResourceController extends BaseController {

    private DicService dicService;
    private BoxService boxService;
    private ResourceService resourceService;

    @Autowired
    public void setDicService(DicService dicService) {
        this.dicService = dicService;
    }

    @Autowired
    public void setResourceService(ResourceService resourceService) {
        this.resourceService = resourceService;
    }

    @Autowired
    public void setBoxService(BoxService boxService) {
        this.boxService = boxService;
    }

    //动画配置列表
    @RequestMapping(path = "/scene/index", name = "动画配置列表")
    public ModelAndView pageList(HttpServletRequest request, HttpServletResponse response) {
        String pStr = request.getParameter("page");//当前页
        Page pg = new Page();
        if (pStr != null && Validator.isNum(pStr)) {
            pg.setPage(Integer.parseInt(pStr));
        }
        String sizeStr = request.getParameter("size");//容量
        if (sizeStr != null && Validator.isNum(sizeStr)) {
            pg.setSize(Integer.parseInt(sizeStr));
        }
        boxService.getSceneList(pg);
        ModelAndView mv = new ModelAndView();
        mv.addObject("page", pg);
        return mv;

    }

    //查询一个scene
    @RequestMapping(path = "/scene/form", name = "编辑动画配置")
    public ModelAndView form(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        Scene scene = boxService.getById(id);
        if (scene == null) {
            scene = new Scene();
        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("scene", scene);
        return mv;
    }

    @RequestMapping(path = "/scene/update", name = "保存动画配置")
    public Result updateScene(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        Scene scene = new Scene();
        scene.setId(id);
        scene.setSceneNumber(req.getParameter("sceneNumber"));
        String roleStatusStr = req.getParameter("roleStatus");
        int roleStatus = 0;
        if (roleStatusStr != null && Validator.isNum(roleStatusStr)) {
            roleStatus = Integer.parseInt(roleStatusStr);
        }
        scene.setRoleStatus(roleStatus);
        scene.setSceneOrder(req.getParameter("sceneOrder"));

        String resource_idStr = req.getParameter("resource_id");
        int resource_id = 0;
        if (resource_idStr != null && Validator.isNum(resource_idStr)) {
            resource_id = Integer.parseInt(resource_idStr);
        }
        scene.setResource_id(resource_id);

        scene.setAudioUrl(req.getParameter("audioUrl"));
        scene.setRelation_number(req.getParameter("relation_number"));

        String playTimeStr = req.getParameter("playTime");
        int playTime = 0;
        if (playTimeStr != null && Validator.isNum(playTimeStr)) {
            playTime = Integer.parseInt(playTimeStr);
        }
        scene.setPlayTime(playTime);

        String audioTimeStr = req.getParameter("playTime");
        Float audioTime = 0F;
        if (audioTimeStr != null && Validator.isNum(audioTimeStr)) {
            audioTime = Float.parseFloat(req.getParameter("audioTime"));
        }
        scene.setAudioTime(audioTime);
        scene.setDescribe(req.getParameter("describe"));

        int rst = boxService.saveScene(scene);
        Result rs = ok();
        if (rst > 0) {
            rs.setMsg("保存成功!");
            rs.setCode(1);
        } else {
            rs.setMsg("保存失败!");
            rs.setCode(0);
        }
        return rs;
    }

    //删除Scene
    @RequestMapping(path = "/deleteScene", name = "删除动画配置")
    public Result deleteScene(HttpServletRequest request) {
        int sceneId = Integer.parseInt(request.getParameter("id"));
        int rst = boxService.deleteScene(sceneId);
        Result rs = ok();
        if (rst > 0) {
            rs.setMsg("删除成功!");
            rs.setCode(1);
        } else {
            rs.setMsg("删除失败!");
            rs.setCode(0);
        }
        return rs;
    }


    @RequestMapping(path = "/updateSelected", name = "修改精选")
    public Result updateSelected(HttpServletRequest req) {
        //修改精选
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }

        String selectedStr = req.getParameter("is_selected");
        int is_selected = Integer.parseInt(selectedStr);
        int rs = resourceService.updateSelected(id, is_selected);
        if (rs > 0) {
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败！");
        }
    }

    @RequestMapping(path = "/index", name = "影片分页列表")
    public ModelAndView resouceIndex(HttpServletRequest req, HttpServletResponse res) {
        String pStr = req.getParameter("page");
        Page p = new Page();
        if (pStr != null && Validator.isNum(pStr)) {
            p.setPage(Integer.parseInt(pStr));
        }
        String sizeStr = req.getParameter("size");
        if (sizeStr != null && Validator.isNum(sizeStr)) {
            p.setSize(Integer.parseInt(sizeStr));
        }

        ModelAndView mv = new ModelAndView();
        Map<String, String> param = new HashMap<String, String>();
        String search_param = "";
        String title = req.getParameter("title");
        if (title != null && title.length() > 0) {
            param.put("title", "%" + title + "%");
            search_param += "&title=" + title;
            mv.addObject("title", title);
        }
        String source = req.getParameter("source");
        if (source != null && source.length() > 0) {
            param.put("source", source);
            search_param += "&source=" + source;
            mv.addObject("source", Integer.valueOf(source));
        }
        String region = req.getParameter("region");
        if (region != null && region.length() > 0) {
            param.put("region", region);
            search_param += "&region=" + region;
            mv.addObject("region", Integer.valueOf(region));
        }
        String language = req.getParameter("language");
        if (language != null && language.length() > 0) {
            param.put("language", language);
            search_param += "&language=" + language;
            mv.addObject("language", Integer.valueOf(language));
        }
        String type = req.getParameter("type");
        if (type != null && type.length() > 0) {
            param.put("type", type);
            search_param += "&type=" + type;
            mv.addObject("type", Integer.valueOf(type));
        }


        resourceService.findResoucePage(p, param);

        List<Map<String, Object>> region_list = dicService.getDicList("170");
        List<Map<String, Object>> language_list = dicService.getDicList("188");
        List<Map<String, Object>> source_list = dicService.getDicList("194");
        List<Map<String, Object>> type_list = dicService.getDicList("173");
        mv.addObject("region_list", region_list);
        mv.addObject("source_list", source_list);
        mv.addObject("language_list", language_list);
        mv.addObject("type_list", type_list);
        mv.addObject("page", p);
        mv.addObject("search_param", search_param);
        return mv;

    }

    @RequestMapping(path = "/selection_list", name = "精选影片列表")
    public ModelAndView selection_list(HttpServletRequest req, HttpServletResponse res) {
        String pStr = req.getParameter("page");
        Page p = new Page();
        if (pStr != null && Validator.isNum(pStr)) {
            p.setPage(Integer.parseInt(pStr));
        }
        String sizeStr = req.getParameter("size");
        if (sizeStr != null && Validator.isNum(sizeStr)) {
            p.setSize(Integer.parseInt(sizeStr));
        }

        ModelAndView mv = new ModelAndView();
        Map<String, String> param = new HashMap<String, String>();
        String search_param = "";
        String title = req.getParameter("title");
        if (title != null && title.length() > 0) {
            param.put("title", "%" + title + "%");
            search_param += "&title=" + title;
            mv.addObject("title", title);
        }
        String source = req.getParameter("source");
        if (source != null && source.length() > 0) {
            param.put("source", source);
            search_param += "&source=" + source;
            mv.addObject("source", Integer.valueOf(source));
        }
        String region = req.getParameter("region");
        if (region != null && region.length() > 0) {
            param.put("region", region);
            search_param += "&region=" + region;
            mv.addObject("region", Integer.valueOf(region));
        }
        String language = req.getParameter("language");
        if (language != null && language.length() > 0) {
            param.put("language", language);
            search_param += "&language=" + language;
            mv.addObject("language", Integer.valueOf(language));
        }
        String type = req.getParameter("type");
        if (type != null && type.length() > 0) {
            param.put("type", type);
            search_param += "&type=" + type;
            mv.addObject("type", Integer.valueOf(type));
        }


        resourceService.findResoucePage(p, param);

        List<Map<String, Object>> region_list = dicService.getDicList("170");
        List<Map<String, Object>> language_list = dicService.getDicList("188");
        List<Map<String, Object>> source_list = dicService.getDicList("194");
        List<Map<String, Object>> type_list = dicService.getDicList("173");

        mv.addObject("region_list", region_list);
        mv.addObject("source_list", source_list);
        mv.addObject("language_list", language_list);
        mv.addObject("type_list", type_list);
        mv.addObject("page", p);
        mv.addObject("search_param", search_param);
        return mv;

    }

    @RequestMapping(path = "/dataIndex", name = "影片视频列表")
    public ModelAndView dataList(HttpServletRequest req, HttpServletResponse res) {
        String pStr = req.getParameter("page");
        Page p = new Page();
        if (pStr != null && Validator.isNum(pStr)) {
            p.setPage(Integer.parseInt(pStr));
        }
        String sizeStr = req.getParameter("size");
        if (sizeStr != null && Validator.isNum(sizeStr)) {
            p.setSize(Integer.parseInt(sizeStr));
        }

        ModelAndView mv = new ModelAndView();
        String resource_id_str = req.getParameter("resource_id");
        if (resource_id_str == null || !Validator.isNum(resource_id_str)) {
            resource_id_str = "0";
        }
        int resource_id = Integer.parseInt(resource_id_str);

        resourceService.findResouceDataPage(p, resource_id);

        mv.addObject("page", p);
        mv.addObject("resource_id", resource_id);
        return mv;

    }

    @RequestMapping(path = "/edit", name = "编辑影片")
    public ModelAndView edit(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }

        Resources resource = resourceService.findById(id);
        if (resource == null) {
            resource = new Resources();
        }
        ModelAndView mv = new ModelAndView();
        List<Map<String, Object>> region_list = dicService.getDicList("170");
        List<Map<String, Object>> language_list = dicService.getDicList("188");
        List<Map<String, Object>> source_list = dicService.getDicList("194");
        List<Map<String, Object>> type_list = dicService.getDicList("173");
        List<Map<String, Object>> age_list = dicService.getDicList("201");
        List<Map<String, Object>> channel_list = dicService.getDicList("207");
        List<Map<String, Object>> school_list = dicService.getDicList("374");
        List<Map<String, Object>> program_list = dicService.getDicList("388");
        mv.addObject("region_list", region_list);
        mv.addObject("source_list", source_list);
        mv.addObject("language_list", language_list);
        mv.addObject("type_list", type_list);
        mv.addObject("age_list", age_list);
        mv.addObject("channel_list", channel_list);
        mv.addObject("school_list", school_list);
        mv.addObject("program_list", program_list);

        mv.addObject("obj", resource);
        return mv;
    }

    @RequestMapping(path = "/save", name = "保存影片")
    public Result save(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        String title = req.getParameter("title");
        String subtitle = req.getParameter("subtitle");
        String region = req.getParameter("region");
        String language = req.getParameter("language");
        String source = req.getParameter("source");
        String year = req.getParameter("year");
        String channel = req.getParameter("channel");
        String type = req.getParameter("type");
        String age = req.getParameter("age");
        String selected = req.getParameter("selected");
        String school = req.getParameter("school");
        String program = req.getParameter("program");
        String describe = req.getParameter("describe");
        String img = req.getParameter("img");
        String ipname = req.getParameter("ipname");
        String ipimg = req.getParameter("ipimg");
        boolean isSelected = false;
        if (selected != null && "1".equals(selected)) {
            isSelected = true;
        }
        String status = req.getParameter("status");

        Resources resource = new Resources();
        resource.setId(id);
        resource.setTitle(title);
        resource.setSubtitle(subtitle);
        resource.setSource(Integer.parseInt(source));
        resource.setLanguage(Integer.parseInt(language));
        resource.setRegion(Integer.parseInt(region));
        resource.setYear(Integer.parseInt(year));
        resource.setAge(Integer.parseInt(age));
        resource.setType(Integer.parseInt(type));
        resource.setSelected(isSelected);
        resource.setSchool(Integer.parseInt(school));
        resource.setProgram(Integer.parseInt(program));
        resource.setDescribe(describe);
        resource.setImg(img);
        resource.setChannel(Integer.parseInt(channel));
        resource.setIpname(ipname);
        resource.setIpimg(ipimg);

        int rs = resourceService.save(resource);

        if (rs > 0) {
            if ("1".endsWith(status)) {
                resourceService.add_index(id);
            }
            return error(1, "保存成功！");
        } else {
            return error(0, "保存失败！");
        }
    }

    @RequestMapping(path = "/del", name = "删除影片")
    public Result del(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        int rs = resourceService.del(id);
        if (rs > 0) {
            resourceService.del_index(id);
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败！");
        }
    }

    @RequestMapping(path = "/updateStatus", name = "上下线影片")
    public Result updateStatus(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        String statusStr = req.getParameter("status");
        int status = 0;
        if (statusStr != null && Validator.isNum(statusStr)) {
            status = Integer.parseInt(statusStr);
        }

        int rs = resourceService.updateStatus(id, status);
        if (rs > 0) {
            if (status == 1) {
                resourceService.add_index(id);
            } else {
                resourceService.del_index(id);
            }
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败！");
        }
    }

    @RequestMapping(path = "/editData", name = "编辑视频")
    public ModelAndView editData(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        String ridStr = req.getParameter("resource_id");
        int resource_id = 0;
        if (ridStr != null && Validator.isNum(ridStr)) {
            resource_id = Integer.parseInt(ridStr);
        }

        ResourceData resourceData = resourceService.findDataById(id);
        if (resourceData == null) {
            resourceData = new ResourceData();
            resourceData.setResourceId(resource_id);
        }
        ModelAndView mv = new ModelAndView();

        mv.addObject("obj", resourceData);
        return mv;
    }

    @RequestMapping(path = "/saveData", name = "保存视频")
    public Result saveData(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        String title = req.getParameter("title");
        String url = req.getParameter("url");
        String img = req.getParameter("img");
        String lengthStr = req.getParameter("length");
        String resourceId = req.getParameter("resourceId");
        int length = 0;
        if (lengthStr != null && Validator.isNum(lengthStr)) {
            length = Integer.parseInt(lengthStr);
        }

        ResourceData r = new ResourceData();
        r.setId(id);
        r.setTitle(title);
        r.setUrl(url);
        r.setImg(img);
        r.setLength(length);
        r.setResourceId(Integer.parseInt(resourceId));
        r.setSort(1);
        int rs = resourceService.saveData(r);

        if (rs > 0) {
            return error(1, "保存成功！");
        } else {
            return error(0, "保存失败！");
        }
    }

    @GetMapping(path = "/playLog", name = "影片播放记录")
    public ModelAndView playLogList(HttpServletRequest req, HttpServletResponse res) {
        String pStr = req.getParameter("page");
        Page p = new Page();
        if (pStr != null && Validator.isNum(pStr)) {
            p.setPage(Integer.parseInt(pStr));
        }
        String sizeStr = req.getParameter("size");
        if (sizeStr != null && Validator.isNum(sizeStr)) {
            p.setSize(Integer.parseInt(sizeStr));
        }

        String login = req.getParameter("login");
        String title = req.getParameter("title");
        Map<String, String> param = new HashMap<String, String>();
        ModelAndView mv = new ModelAndView();
        String search_param = "";
        if (login != null && login.length() > 0) {
            param.put("login", "%" + login + "%");
            mv.addObject("search_login", login);
            search_param += "&login=" + login;
        }
        if (title != null && title.length() > 0) {
            param.put("title", "%" + title + "%");
            mv.addObject("search_title", title);
            search_param += "&title=" + title;
        }

        resourceService.resourcePlayLogs(p, param);

        mv.addObject("page", p);
        mv.addObject("search_param", search_param);
        return mv;

    }

    @RequestMapping(path = "/delData", name = "删除视频")
    public Result delData(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        int rs = resourceService.delData(id);
        if (rs > 0) {
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败！");
        }
    }

    @RequestMapping(path = "/selectResource", produces = "text/html; charset=UTF-8")
    public ModelAndView selectResource() {
        return new ModelAndView("/resource/selectResource");
    }

    @RequestMapping(path = "/select_school_resource", produces = "text/html; charset=UTF-8")
    public ModelAndView selectSchoolResource() {
        return new ModelAndView("/resource/select_school_resource");
    }

    @RequestMapping(path = "/selectResourceData", produces = "application/json; charset=UTF-8")
    public Map<String, Object> selectResourceData(@RequestParam(name = "page", required = false, defaultValue = "1") int page, @RequestParam(name = "limit", required = false, defaultValue = "10") int size, HttpServletRequest req, HttpServletResponse res) {
        Page p = new Page();
        p.setPage(page);
        p.setSize(size);

        String is_schoolStr = req.getParameter("is_school");
        int is_school = 0;
        if (is_schoolStr != null && Validator.isNum(is_schoolStr)) {
            is_school = Integer.parseInt(is_schoolStr);
        }
        p.setOther(is_school);
        resourceService.findPage(p);
        Map<String, Object> rs = new HashMap<String, Object>();
        rs.put("code", 0);
        rs.put("count", p.getCount());
        rs.put("data", p.getData());
        rs.put("msg", "");
        return rs;
    }

    @RequestMapping(path = "/sortData", name = "视频排序")
    public Result sortData(HttpServletRequest req) {
        int id = Integer.parseInt(req.getParameter("id"));
        String action = req.getParameter("action");
        int rs = resourceService.sortData(id, action);
        if (rs > 0) {
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败！");
        }
    }
}
